import React, { Component } from 'react';
import addclass from './index.module.css'
import { Select, Input, Button, message } from 'antd';
const { Option } = Select;
class AddClass extends Component {
  state = {
    college: '全部',
    course: '全部',
    Collegedata: [],
    Coursedata: [],
    userData: [],
	teacherdata:[],
	coachdata:[],
    className: '',
    teacherid:'未选择',
    coachid: '未选择',
    classType: '40'

  }
  getDatas = (value) => {
	   let { Collegedata} = this.state
	   this.setState({ college: value })
	   let fillterData = Collegedata.find(item => item.schoolid === value).courseList
	   this.setState({course:fillterData[0].txt})
	   this.setState({ Coursedata: fillterData })
  }
  initData = () => {
    this.$httpReq(this.$api.getSchoolList(), res => {
      this.setState({ Collegedata: res.data })
    })
  }
  addClasses = () => {
    let { className, college, teacherid, coachid, course, classType} = this.state
    if (className === '' || college === '' || teacherid === '未选择' || coachid === '' || course === '' || classType === '') {
      return message.error('请将数据补充完整')
    }
    if (college === '全部' || course === '全部') {
      return message.warning('请选择学院和课程')
    }
	console.log(className,college,teacherid,coachid,course,classType)
	this.$httpReq(this.$api.addClass({className, school:college, teacherid, coachid, course, classType}),res=>{
		if(res.code===-2) return message.warning(res.msg)
		if(res.code===1){
			message.success(res.msg)
			this.props.history.push('/home/class/classList')
			return 
		}
		if(res.code===-1) return message.error(res.msg)
	})
  }
  
  render() {
    const { Collegedata, Coursedata, college, course, className, teacherid, coachid, classType,teacherdata,coachdata } = this.state
    return <div className={addclass.box}>
      <div><span className={addclass.getspan}>班级名称:</span><Input placeholder="请输入班级名称" style={{ width: 320 }} value={className} onChange={(ev) => this.setState({ className: ev.target.value })} className={addclass.dinginp} /></div>
      <div><span className={addclass.getspan}>所属学院:</span><Select style={{ width: 320 }} value={college} onChange={(value) => this.getDatas(value)} className={addclass.dinginp}>
        {
          Collegedata && Collegedata.map((item,index) => {
            return <Option key={index} value={item.schoolid}>{item.schoolid}</Option>
          })
        }
      </Select>
      </div>
	  <div>
	  		<span className={addclass.getspan}>现阶段课程:</span><Select style={{ width: 320 }} value={course} onChange={(value) => this.setState({ course: value })} className={addclass.dinginp}>
	    {
	      Coursedata && Coursedata.map((item,index) => {
	        return <Option key={index} value={item.txt}>{item.txt}</Option>
	      })
	    }
	  </Select>
	  </div>
      <div>
		<span className={addclass.getspan}>现阶段辅导员:</span>
		<Select value={teacherid} onChange={(value)=>this.setState({teacherid:value})} style={{ width: 320 }}  className={addclass.dinginp}>
			{
				teacherdata.map((item,index)=>{
					return <Option  key={index} value={item.userid}>{item.name}</Option>
				})
			}
		</Select>
	  </div>
      <div>
		<span className={addclass.getspan}>授课老师:</span>
		<Select value={coachid} onChange={(value)=>this.setState({coachid:value})} style={{ width: 320 }}  className={addclass.dinginp}>
			{
				coachdata.map((item,index)=>{
					return <Option  key={index} value={item.userid}>{item.name}</Option>
				})
			}		
		</Select>
	  </div>
      <div>
		<span className={addclass.getspan}>班级类型:</span><Select value={classType} style={{ width: 320 }} onChange={(value) => this.setState({ classType: value })} className={addclass.dinginp}>
        <Option value="40">大班</Option>
        <Option value="30">中班</Option>
        <Option value="20">小班</Option>
      </Select>
      </div>
      <Button  type="primary" style={{ width: '100px', margin: '25px' }} onClick={() => this.addClasses()}>添加班级</Button>
    </div>
  }
  componentDidMount() {
	 this.initData()
	this.$httpReq(this.$api.userList(), res => {
		this.setState({ userData: res.data })
	})
	
	this.$httpReq(this.$api.getTeacherList(), res => {
		this.setState({teacherdata:res.data})
	})
	this.$httpReq(this.$api.getCoachList(), res => {
		this.setState({coachdata:res.data})
	})
  }
}
export default AddClass
